package com.tkjt.pms.modules.common.controller;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tkjt.pms.common.utils.InfoAttrUtil;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.system.entity.SysConfig;
import org.jeecg.modules.system.mapper.SysConfigMapper;
import org.jeecg.modules.system.service.ISysConfigService;
import org.jeecg.modules.system.service.impl.SysConfigServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.tkjt.pms.common.vos.TaskStatusVo;
import com.tkjt.pms.modules.common.service.PMSCommonService;
import com.tkjt.pms.modules.common.service.PMSTaskService;


@RestController
@RequestMapping("/api/pms/common")
public class PMSCommonController {
	
    @Autowired
    private PMSCommonService commonService;
    @Autowired
    private PMSTaskService pmsTaskService;

    
    /**
     * 评分年月信息
     * @return
     */
    @RequestMapping(value = "yearMonthList",method = RequestMethod.GET)
    public Result<?> yearMonthList(){
        try {
            return Result.ok(commonService.yearMonthList());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }
    /**
     * 评分年份信息
     * @return
     */
    @RequestMapping(value = "checkYearList",method = RequestMethod.GET)
    public Result<?> checkYearList(){
        try {
            return Result.ok(commonService.checkYearList());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }

    /**
     * 部门类别与部门级联关系
     * @return
     */
    @RequestMapping(value = "indexGroupOrgs",method = RequestMethod.GET)
    public Result<?> indexGroupOrgs(){
        try {
            return Result.ok(commonService.getOrgCodeCG());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }


    /**
     * 获取所有本门
     * @return
     */
    @RequestMapping(value = "getAllOrgs",method = RequestMethod.GET)
    public Result<?> getAllOrgs(){
        try {
            return Result.ok(commonService.getAllOrgs());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }

    /**
     * 被评分部门与评分部门级联关系
     * @return
     */
    @RequestMapping(value = "gradeOrgs",method = RequestMethod.GET)
    public Result<?> gradeOrgs(){
        try {
            return Result.ok(commonService.gradeOrgs());
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }

    /**
     *
     * 根据指标值，查询对应指标信息
     * @return
     */
    @RequestMapping(value = "indexValueCodes")
    public Result<?> indexValueCodes(String indexCode,String indexGroup){
        try {
            return Result.ok(commonService.indexValueCodes(indexGroup,indexCode));
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }

    /**
     * 根据组织类型，查询对应指标信息
     * @return
     */
    @RequestMapping(value = "indexCodes")
    public Result<?> indexCodes(String indexGroup,String indexType){
        try {
            return Result.ok(commonService.indexCodes(indexGroup,indexType,null,null,null));
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }

    /**
     * 绩效填报  根据组织类型，查询对应指标信息
     * @return
     */
    @RequestMapping(value = "canEditIndexCodes")
    public Result<?> canEditIndexCodes(String indexGroup,String indexType){
        try {
            return Result.ok(commonService.indexCodes(indexGroup,indexType,"1",null,null));
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }

    /**
     * 绩效目标值  根据组织类型，查询对应指标信息
     * @return
     */
    @RequestMapping(value = "targetIndexCodes")
    public Result<?> targetIndexCodes(String indexGroup,String indexType){
        try {
            List<String> attrs = new ArrayList<>();
            attrs.add(InfoAttrUtil.MU_BIAO_ZHI);
            return Result.ok(commonService.indexCodes(indexGroup,indexType,null, attrs,null));
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }

    /**
     * 绩效权重  根据组织类型，查询对应指标信息
     * @return
     */
    @RequestMapping(value = "weightIndexCodes")
    public Result<?> weightIndexCodes(String indexGroup,String indexType){
        try {
            List<String> attrs = new ArrayList<>();
            attrs.add(InfoAttrUtil.XIAO_JI);
            attrs.add(InfoAttrUtil.ZONG_JI);
            return Result.ok(commonService.indexCodes(indexGroup,indexType,null, null,attrs));
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }

    /**
     * 根据组织类型，查询对应下属单位信息
     * @return
     */
    @RequestMapping(value = "cgOrgs")
    public Result<?> cgOrgs(String indexGroup){
        try {
            return Result.ok(commonService.cgOrgs(indexGroup));
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("获取数据异常");
        }
    }

    /**
     * 组织类型(指标归属)信息接口
     * @return
     */
    @RequestMapping(value = "orgTypes")
    public Result<?> orgTypes(){
        List<Map<String,String>> list=new ArrayList<>();
        Map<String,String> map=new HashMap<>();
        map.put("name","一线单位");
        map.put("code","C");
        list.add(map);
        map=new HashMap<>();
        map.put("name","养康本部");
        map.put("code","G");
        list.add(map);
        Map<String,Object> orgTypes=new HashMap<>();
        orgTypes.put("orgTypes",list);
        return Result.ok(orgTypes);
    }

    /**
     * 组织类型(指标归属)(包含全部)信息接口
     * @return
     */
    @RequestMapping(value = "orgTypesAll")
    public Result<?> orgTypesAll(){
        List<Map<String,String>> list=new ArrayList<>();
        Map<String,String> map=new HashMap<>();
        map.put("name","全部");
        map.put("code","");
        list.add(map);
        map=new HashMap<>();
        map.put("name","一线单位");
        map.put("code","C");
        list.add(map);
        map=new HashMap<>();
        map.put("name","养康本部");
        map.put("code","G");
        list.add(map);
        Map<String,Object> orgTypes=new HashMap<>();
        orgTypes.put("orgTypes",list);
        return Result.ok(orgTypes);
    }
    
    /**
     * *计算状态
     * @return
     */
    @RequestMapping(value = "taskStatus", method = RequestMethod.POST)
    public Result<?> taskStatus(@RequestBody TaskStatusVo vo){
    	if(StringUtils.isEmpty(vo.getTaskCode()) || StringUtils.isEmpty(vo.getIndexYearMonth())) {
    		return Result.error("请求参数不对！");
    	}
    	Map<String, String> retMap = pmsTaskService.getTaskStatus(vo.getTaskCode(), vo.getIndexGroup(), vo.getIndexYearMonth());
    	return Result.ok(retMap);
    }

    /**
     * 显示上一年度所有月份 与  当年 1月至当前时间月份
     * 列表月份按 时间倒序显示
     */
    @RequestMapping(value = "allYearMonthList",method = RequestMethod.GET)
    public Result<?> allYearMonthList(){
        List<Map<String,String>> list=new ArrayList<>();
        Calendar instance = Calendar.getInstance();
        int lastYear = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH);
        for(int i=1; i <=12;i++){
            Map<String,String> map=new HashMap<>();
            int year = lastYear - 1;
            String monStr;
            if(i > 10){
                monStr = i+"";
            }else {
                monStr = "0"+i;
            }
            map.put(year+monStr,year+"年"+monStr+"月");
            list.add(map);
        }

        for (int i = 1; i <= month + 1; i++) {
            String monStr;
            if(i > 10){
                monStr = i+"";
            }else {
                monStr = "0"+i;
            }
            Map<String,String> map=new HashMap<>();
            map.put(lastYear+monStr,lastYear+"年"+monStr+"月");
            list.add(map);
        }
        return Result.ok(list);
    }
    /**
     * *绩效计算审核添加筛选框【维度】查询
     * @return
     */
    @RequestMapping(value = "indexAttrs", method = RequestMethod.POST)
    public Result<?> indexAttrs(){
        List<Map<String,String>> list = new ArrayList<>();
        Map<String,String> map=new HashMap<>();
        map.put("name","目标值");
        map.put("code",InfoAttrUtil.MU_BIAO_ZHI);
        list.add(map);
        map=new HashMap<>();
        map.put("name","实际值");
        map.put("code",InfoAttrUtil.SHI_JI_ZHI);
        list.add(map);
        map=new HashMap<>();
        map.put("name","达成率");
        map.put("code",InfoAttrUtil.DA_CHENG_LV);
        list.add(map);
        map=new HashMap<>();
        map.put("name","得分");
        map.put("code",InfoAttrUtil.DE_FEN);
        list.add(map);
        map=new HashMap<>();
        map.put("name","小计");
        map.put("code",InfoAttrUtil.XIAO_JI);
        list.add(map);
        map=new HashMap<>();
        map.put("name","总计");
        map.put("code",InfoAttrUtil.ZONG_JI);
        list.add(map);
        return Result.ok(list);
    }

    /**
     * *获取sys_config配置信息
     * @return
     */
    @RequestMapping(value = "getSysConfig")
    public Result<?> getSysConfig(String configCode){
        if(StringUtils.isEmpty(configCode)){
            return Result.error("参数缺失！");
        }
        return Result.ok(commonService.getSysConfig(configCode));
    }

    /**
     * *获取sys_config配置信息
     * @return
     */
    @RequestMapping(value = "userReportDepartList" , method = RequestMethod.GET)
    public Result<?> userReportDepartList(@RequestParam(name = "userId", required = true) String userId){
        if(StringUtils.isEmpty(userId)){
            return Result.error("参数缺失！");
        }
        return Result.ok(commonService.userReportDepartList(userId));
    }
}
